"""
Case Type   : 共享存储-工具-系统内部工具-pg_resetxlog
Case Name   : 共享存储数据库下，不指定--vgname参数
Create At   : 2023/4/24
Owner       : opentestcase019
Description :
    1.查看数据库状态，若为开启状态则关闭数据库;
    2.执行pg_resetxlog命令，指定错误的参数
Expect      :
    1.成功
    2.合理报错
History     :
"""
import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.node_num = self.primary_sh.get_node_num()

    def test_sharestorage_tool(self):
        step1 = "-----step1:查看数据库状态，若为开启状态则关闭数据库;expect:成功-----"
        self.logger.info(step1)
        for i in range(self.node_num, 0, -1):
            stop_msg = self.primary_sh.exec_cm_ctl(
                mode='stop', param=f'-n {i} -D {macro.DB_INSTANCE_PATH}')
            self.logger.info(stop_msg)
            self.assertIn(self.constant.cm_stop_instance_success_msg,
                          stop_msg, '执行失败' + step1)
        status = self.primary_sh.get_db_cluster_status('detail')
        self.logger.info(status)
        self.assertEquals(self.node_num, status.count("Manually stopped"),
                          "执行失败" + step1)

        step2 = "-----step2:执行pg_resetxlog命令，指定错误的参数;expect:合理报错-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"pg_resetxlog {macro.DB_INSTANCE_PATH} " \
            f"--enable-dss --socketpaths={macro.UDS_PATH} --vgname=+data;" \
            f"pg_resetxlog {macro.DB_INSTANCE_PATH} " \
            f"--enable-dss --socketpath={macro.UDS_PATH} ---vgname=+data;" \
            f"pg_resetxlog {macro.DB_INSTANCE_PATH} " \
            f"--enable-dss --socketpath={macro.UDS_PATH} --vgname=+data -s"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"pg_resetxlog: unrecognized option "
                      f"'--socketpaths={macro.UDS_PATH}'",
                      execute_msg, "执行失败" + step2)
        self.assertIn("pg_resetxlog: unrecognized option '---vgname=+data'",
                      execute_msg, "执行失败" + step2)
        self.assertIn("pg_resetxlog: invalid option -- 's'",
                      execute_msg, "执行失败" + step2)

    def tearDown(self):
        self.logger.info("-----恢复环境-----")
        for i in range(1, self.node_num + 1):
            start_msg = self.primary_sh.exec_cm_ctl(
                mode='start', param=f'-n {i} -D {macro.DB_INSTANCE_PATH}')
            self.logger.info(start_msg)
            self.assertIn(self.constant.cm_start_instance_success_msg,
                          start_msg, '恢复环境失败')
        time.sleep(30)
        status = self.primary_sh.get_db_cluster_status('detail')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "恢复环境失败")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
